let ajax = getApp().globalData.ajax;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    skip:0,
    take: 5,
    hasNext: false,
    orderList: [],
    genderSrc: 'https://bpic.588ku.com/element_pic/17/12/15/13b4972f963b5ffeb46693fc9f0fc4b1.jpg!/fw/254/quality/90/unsharp/true/compress/true',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let json = {
      token: wx.getStorageSync('Cookie'),
      skip: this.data.skip,
      take: this.data.take,
    };
    this.getData(json);
  },
  getData(json) {

    if (!this.data.hasNext){
      ajax('order/list', json).then(res => {
        console.log(res);
        let orderList = res.data;
        if (!orderList.length) {
          this.setData({
            hasNext: true
          })
        }
        orderList.forEach(item => {
          item.createTimeRes = item.create_time.toDateString();
          item.priceRes = (item.price / 100).toFixed(2);
          switch (item.status) {
            case 'INIT':
              item.statusRes = '未支付';
              break;
            case 'BUYER_START':
              item.statusRes = '未支付';
              break;
            case 'PAYED':
              item.statusRes = '已支付';
              break;
            case 'CANCELED':
              item.statusRes = '已取消';
              break;
            case 'READY':
              item.statusRes = '已确认';
              break;
            case 'ONGOING':
              item.statusRes = '进行中';
              break;
            case 'FINISH_AUTO':
              item.statusRes = '已完成';
              break;
            case 'FINISH':
              item.statusRes = '已完成';
              break;
            case 'PAYED':
              item.statusRes = '已支付';
              break;
            case 'PAYED PAY':
              item.statusRes = '已支付';
              break;
            case 'SELLER_START':
              item.statusRes = '已接单';
              break;
            case 'DENY':
              item.statusRes = '已拒绝';
              break;
            case 'REFUND':
              item.statusRes = '自动退款';
              break;
          }
        })
        orderList = this.data.orderList.concat(orderList);
        this.setData({ orderList, })
      })
    }

    
  },

  toDetail(e){
    let orderId = e.currentTarget.dataset.orderid;
    
    wx.navigateTo({
      url: `../orderDetail/orderDetail?id=${orderId}`,
    })
  },

  payInfo(e){
    console.log(e);
    let order_no = e.currentTarget.dataset.orderno;
    let json = {
      token: wx.getStorageSync('Cookie'),
      pay_type: 'WEI_XIN',
      order_type: 'Order',
      order_no,
    }
    ajax('pay/info', json).then(res => {

      let msg = res.data.msg;
      console.log(res);
      wx.requestPayment({
        timeStamp: msg.timeStamp,
        nonceStr: msg.nonceStr,
        package: msg.package,
        signType: 'MD5',
        paySign: msg.paySign,
        success() {
          wx:wx.showToast({
            title: '支付成功',
            icon: 'success',
            image: '',
            duration: 0,
            mask: true,
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        },
        fail(err) {
          console.log(err);
        },
        complete() {
          console.log({
            timeStamp: msg.timeStamp,
            nonceStr: msg.nonceStr,
            package: msg.package,
            signType: 'MD5',
            paySign: msg.paySign,
          })
        }
      })


    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
    // if (!this.data.hasNext) {
    //   console.log('没有更多数据')
    //   return
    // }
    let skip = this.data.skip;
    skip += this.data.take;
    this.setData({
      skip,
    })
    let json = {
      token: wx.getStorageSync('Cookie'),
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})